package com.zik.lredisdemo.config;

import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.util.StringUtils;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;

/**
 * 使用jedis整合redis
 */
@Slf4j
@Data
@Configuration
public class RedisConfig {

    @Value("${spring.redis.host}")
    private String host;

    @Value("${spring.redis.port}")
    private int port;

    @Value("${spring.redis.password}")
    private String password;

    @Value("${spring.redis.connect-timeout}")
    private int timeout;

    @Bean
    public JedisPool redisPoolFactory() {
        log.info("JedisPool 注入成功！");
        log.info("redis地址：" + host + ":" + port);
        JedisPoolConfig jedisPoolConfig = new JedisPoolConfig();
        //是否启用pool的jmx管理功能，默认true
        jedisPoolConfig.setJmxEnabled(true);
        JedisPool JedisPool;
        if (StringUtils.hasText(password)) {
            JedisPool = new JedisPool(jedisPoolConfig, host, port, timeout, password);
        } else {
            JedisPool = new JedisPool(jedisPoolConfig, host, port, timeout);
        }
        log.info("JedisPool：" + JedisPool);
        return JedisPool;
    }
}


